/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings;

import com.hisense.settings.util.Wrapper;
import java.net.InetAddress;
import java.util.List;


import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;

import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.SeekBarDialogPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.text.Editable;
import android.text.TextWatcher;

import android.util.Log;
import android.view.LayoutInflater;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.WindowManager;






public class BrightnessPreference extends SeekBarDialogPreference implements
        SeekBar.OnSeekBarChangeListener{

	private static final String TAG = "BrightnessPreference";

    private SeekBar mSeekBar;
	private TextView mBrightNumView;

    private int mOldBrightness;

    private boolean mRestoredOldState;
    private Context mContext;

	private static final int MINIMUM_BRIGHT = 0;
	private static final int MAXIMUM_BRIGHT = 100;

    private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            //onBrightnessChanged();
            Log.d(TAG, "onChange -------");
        }
    };
 

    public BrightnessPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "BrightnessPreference -------");
        setDialogLayoutResource(R.layout.preference_dialog_brightness);
        setDialogIcon(R.drawable.ic_settings_display);
		mContext = context;
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);
        Log.d(TAG, "showDialog -------");
        //getContext().getContentResolver().registerContentObserver(
        //        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true,
        //        mBrightnessObserver);

        mRestoredOldState = false;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        Log.d(TAG, "onBindDialogView -------");
        //mSeekBar = getSeekBar(view);
        mSeekBar = (SeekBar)view.findViewById(R.id.seekbar);
        mSeekBar.setMax(MAXIMUM_BRIGHT - MINIMUM_BRIGHT);
		mSeekBar.setKeyProgressIncrement(1);
        mOldBrightness = getBrightness();
        mSeekBar.setProgress(mOldBrightness - MINIMUM_BRIGHT);
        mSeekBar.setOnSeekBarChangeListener(this);
		mBrightNumView = (TextView)view.findViewById(R.id.brightness_value);
		mBrightNumView.setText(""+mOldBrightness);

		
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        Log.d(TAG, "onProgressChanged -------");
		setBrightness(progress);
		mBrightNumView.setText(""+progress);
		//callChangeListener(progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // NA
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // NA
    }

    private int getBrightness() {
        int brightness = 0;
		brightness = Wrapper.getWrapperInstance().getBrightNess();										
        return brightness;
    }


    private void onBrightnessChanged() {
        int brightness = getBrightness();
        mSeekBar.setProgress(brightness - MINIMUM_BRIGHT);
        Log.d(TAG, "onBrightnessChanged -------");
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        final ContentResolver resolver = getContext().getContentResolver();

        if (positiveResult) {
        Log.d(TAG, "onDialogClosed -------positiveResult");
            //Settings.System.putInt(resolver,
            //        Settings.System.SCREEN_BRIGHTNESS,
            //        mSeekBar.getProgress() + MINIMUM_BRIGHT);
        } else {
            restoreOldState();
			Log.d(TAG, "onDialogClosed*****onDialogClosed " 
				+ "returnkeyreturnkey = " + Wrapper.getWrapperInstance().getPictureMode());
        }
		Log.d(TAG, "onDialogClosed*****onDialogClosed " 
				+ "currentImageMode = " + Wrapper.getWrapperInstance().getPictureMode());

		callChangeListener(getBrightness());
		//Wrapper.getWrapperInstance().setPictureMode(SCMPQSettingsEnums.SCMPictureModes.SCM_PICTURE_MODE_PRO1.ordinal());
        resolver.unregisterContentObserver(mBrightnessObserver);
    }

    private void restoreOldState() {
        Log.d(TAG, "restoreOldState -------");
        if (mRestoredOldState) return;
        Log.d(TAG, "restoreOldState -------notreturn");
        setBrightness(mOldBrightness);
        mRestoredOldState = true;
    }

    private void setBrightness(int brightness) {
        Wrapper.getWrapperInstance().setBrightNess(brightness);
        Log.d(TAG, "setBrightness -------");
    }


    @Override
    protected Parcelable onSaveInstanceState() {
        Log.d(TAG, "onSaveInstanceState -------");
        final Parcelable superState = super.onSaveInstanceState();
        if (getDialog() == null || !getDialog().isShowing()) return superState;
        Log.d(TAG, "onSaveInstanceState -------not return");
        // Save the dialog state
        final SavedState myState = new SavedState(superState);
        myState.progress = mSeekBar.getProgress();
        myState.oldProgress = mOldBrightness;

        // Restore the old state when the activity or dialog is being paused
        restoreOldState();
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
        Log.d(TAG, "onRestoreInstanceState -------");
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }
        Log.d(TAG, "onRestoreInstanceState -------not return");
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mOldBrightness = myState.oldProgress;
        setBrightness(myState.progress + MINIMUM_BRIGHT);
    }

    private static class SavedState extends BaseSavedState {

        int progress;
        int oldProgress;

        public SavedState(Parcel source) {
            super(source);
            progress = source.readInt();
            oldProgress = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(progress);
            dest.writeInt(oldProgress);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {

            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}

